import java.util.Arrays;

public class HeapSort {
    public static void main(String[] args) {
        int [] array = {98,34,12,7,8,67,12,78,1};
        HeapSort1(array);
        System.out.println(Arrays.toString(array));
    }

    private static void HeapSort1(int[] array) {
        GreateBigHeap(array);


        int end = array.length-1;
        while (end > 0){
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }

    }

    private static void shiftDown(int[] array, int parent, int length) {
        int child = 2*parent +1;
        while (child < length){
            if(child+1 < length && array[child+1] > array[child]){
                child++;
            }
            if(array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }

        }
    }

    private static void GreateBigHeap(int[] array) {
        for (int parent = (array.length-1-1)/2; parent >= 0 ; parent--) {


        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
}
